//
//  TYCEmoticon.swift
//  Emotions
//
//  Created by tangyunchuan on 2018/7/26.
//  Copyright © 2018年 tangyunchuan. All rights reserved.
//

import UIKit
import YYModel
/// 表情模型
class TYCEmoticon: NSObject {

    /// 表情类型 false - 图片表情 / true
    var type = false
    /// 表情字符串，发送给新浪微博的服务器
    var chs: String?
    /// 表情图片名称，用于本地图文混排
    var png: String?
    /// emoji 的十六进制编码
    var code: String? {
        didSet {
            guard let code = code else {
                return
            }
            let scanner = Scanner(string: code)
            var result: UInt32 = 0
            scanner.scanHexInt32(&result)
            emoji = String(Character(UnicodeScalar(result)!))
        }
    }
    
    /// 表情使用次数
    var times: Int = 0
    /// emoji 的字符串
    var emoji: String?
    
    /// 表情所在的目录路径
    var directory: String?
    
    /// '图片'表情对于的图像
    var image: UIImage?{
        
        //判断表情类型
        if type {
            return nil
        }
        guard let path = Bundle.main.path(forResource: "HMEmoticon.bundle", ofType: nil),
        let bundle = Bundle(path: path),
            let image = UIImage(named: "lxh/lxh_toule.png", in: bundle, compatibleWith: nil) else {
                return nil
        }
        return UIImage(named: "\(directory)/\(png)", in: bundle, compatibleWith: nil)
    }
    func imageText(font: UIFont) -> NSAttributedString {
        
        /// 判断图像是否存在
        guard let image = image else {
            return NSAttributedString(string: "")
        }
        /// 创建文本附件
        let attachement = TYCEmoticonAttachment()
        attachement.chs = chs
        attachement.image = image
        let height = font.lineHeight
        attachement.bounds = CGRect(x: 0, y: -4, width: height, height: height)
        /// 返回图片属性文本
        let attrStrM = NSMutableAttributedString(attributedString: NSAttributedString(attachment: attachement))
        attrStrM.addAttributes([NSAttributedStringKey.font: font], range: NSRange(location: 0, length: 1))
        return attrStrM
    }
    override var description: String{
        return yy_modelDescription();
    }
    
    
    
}
